package com.ocom.mqsubsidy.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Lazy
@Component
@Configuration
public class CouponsDataExportConfig {

    // 业务队列
    public static final String COUPONS_DATA_EXPORT_QUEUE = "coupons_data_export_queue";

    // 业务交换机
    public static final String COUPONS_DATA_EXPORT_EXCHANGE = "coupons_data_export_exchange";

    // 死信队列
    public static final String COUPONS_DATA_EXPORT_DIE_QUEUE = "coupons_data_export_die_queue";
    // 死信交换机
    public static final String COUPONS_DATA_EXPORT_DIE_EXCHANGE = "coupons_data_export_die_exchange";


    //声明一个死信队列用来存放死信消息
    @Bean
    public Queue couponsDataExportDieQueue() {
        return new Queue(COUPONS_DATA_EXPORT_DIE_QUEUE,true,false,false,null);
    }
    //声明一个死信交换机
    @Bean
    public TopicExchange couponsDataExportDieExchange() {
        return new TopicExchange(COUPONS_DATA_EXPORT_DIE_EXCHANGE,true,true);
    }

    //将死信队列和死信的交换机绑定
    @Bean
    public Binding couponsDataExportQueueBindingDieExchange() {
        return BindingBuilder.bind(couponsDataExportDieQueue()).
                to(couponsDataExportDieExchange()).
                with("dieRoutingKeys");
    }

    //声明一个业务队列
    @Bean
    public Queue couponsDataExportQueue() {
        Map<String, Object> args = new HashMap<>();
        // 声明  死信队列Exchange
        args.put("x-dead-letter-exchange", COUPONS_DATA_EXPORT_DIE_EXCHANGE);
        // 设置死信routingKey
        args.put("x-dead-letter-routing-key", "dieRoutingKeys");

        //优先级队列
        args.put("x-max-priority", 100);

        return new Queue(
                COUPONS_DATA_EXPORT_QUEUE,
                true,
                false,
                false, args);
    }

    //声明一个业务交换机
    @Bean
    public TopicExchange couponsDataExportExchange() {
        return new TopicExchange(COUPONS_DATA_EXPORT_EXCHANGE);
    }

    //将业务队列和业务交换机绑定
    @Bean
    public Binding couponsDataExportBindingDirect() {
        return BindingBuilder.bind(couponsDataExportQueue()).to(couponsDataExportExchange()).with("routingKeys");
    }
}
